package com.atguigu.srb.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName: AutoFillTimeMetaObjectHandler
 * Package: com.atguigu.srb.handler
 * Description:
 *
 * @Author 刘强
 * @Create 2024/4/15 15:23
 * @Version 1.0
 */
// mybatisplus帮助我们完成字段的自动填充
@Component
public class AutoFillTimeMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        if(metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime",new Date());
        }
        if(metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if(metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",new Date());
        }
    }
}
